#! /bin/bash

apps=(
RUN2
RUN3
RUN4
)

start()
{
    for app in ${apps[@]}
    do
    tmp=`ps -ef | grep "${app}" | wc -l`
    if [ $tmp -gt 1 ]; then 
      echo "The server arealdy started...abort!" 
      exit 1 
    fi 
    done

    for app in ${apps[@]}
    do
    rm logs/$app.log
    rm errs/$app.log
    echo "${app} stop clean ok"
    sleep 2
    done
    echo 'start all service...'

    for app in ${apps[@]}
    do
    echo 'start' $app
    nohup ./$app/$app 1>logs/$app.log 2>errs/$app.log &
    echo 'ok!'
    sleep 1
    done

    for app in ${apps[@]}
    do
    ps -ef | grep $app
    done
}

stop() {
    for app in ${apps[@]}
    do
    echo ${app}
    k_pid=$(ps -ef | grep ${app} |grep -v grep|awk '{printf $2 "\n"}')
    for i in $k_pid
    do
    echo $i 
    $(kill -9 $i)
    done
    done
}


usage()
{
        echo "Usage: $0 [start|stop|restart]"
}

if [ $# -lt 1 ];then
        usage
        exit
fi

if [ "$1" = "start" ];then
        start

elif [ "$1" = "stop" ];then
        stop

elif [ "$1" = "restart" ];then
        stop
        start

else
        usage
fi








